package com.ling.controller;

import com.ling.bean.Msg;
import com.ling.bean.User;
import com.ling.bean.UserArea;
import com.ling.service.UserAreaService;
import com.ling.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * @author lin
 */
@Controller
public class UserAreaController {
    private final UserAreaService userAreaService;

    @Autowired
    public UserAreaController(UserAreaService userAreaService) {
        this.userAreaService = userAreaService;
    }

    //添加关注领域
    @GetMapping(value = "/addStarArea/{areaId}")
    @ResponseBody
    public Msg addStarArea(HttpSession httpSession, @PathVariable String areaId) {
        User user = (User) httpSession.getAttribute("user");
        if (user == null) {
            return Msg.fail().add("error", "请重新登录！");
        }
        UserArea userArea = new UserArea();
        userArea.setId(StringUtil.getRandomString(16));
        userArea.setUserid(user.getId());
        userArea.setAreaid(areaId);
        userArea.setAbility(0);
        userAreaService.saveUserArea(userArea);
        return Msg.success();
    }

    //取消关注
    @GetMapping(value = "/unstarArea/{areaId}")
    @ResponseBody
    public Msg unstarArea(HttpSession httpSession, @PathVariable String areaId) {
        User user = (User) httpSession.getAttribute("user");
        if (user == null) {
            return Msg.fail().add("error", "请重新登录！");
        }
        userAreaService.deleteUserArea(user.getId(), areaId);
        return Msg.success();
    }

    //获取所有未关注领域
    @GetMapping(value = "/getAllUnstarArea")
    @ResponseBody
    public Msg getAllUnstarArea(HttpSession httpSession) {
        User user = (User) httpSession.getAttribute("user");
        if (user == null) {
            return Msg.fail().add("error", "请重新登录！");
        }
        return Msg.success().add("unstarAreas",userAreaService.getUserUnStarArea(user.getId()));
    }

    //获取关注领域
    @GetMapping(value = "/getUserStarArea")
    @ResponseBody
    public Msg getUserStarArea(HttpSession httpSession) {
        User user = (User) httpSession.getAttribute("user");
        if (user == null) {
            return Msg.fail().add("error", "请重新登录！");
        }
        return Msg.success().add("userAreas", userAreaService.getUserAreasByUserId(user.getId()));
    }
}
